﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;
using Telerik.Web.UI;

public partial class Schools : Acms.AcmsControls.ContentControls.Admin.BaseCustomAdminControl
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        grid.NeedDataSource += new Telerik.Web.UI.GridNeedDataSourceEventHandler(grid_NeedDataSource);
        grid.SortCommand += new Telerik.Web.UI.GridSortCommandEventHandler(grid_SortCommand);
        download.DataBinding += new EventHandler(download_DataBinding);
    }

    void grid_SortCommand(object source, Telerik.Web.UI.GridSortCommandEventArgs e)
    {
        string[] sorts = e.CommandArgument.ToString().Split(',');
        IEnumerable<ClimateChangeChampionsData.School> data = GetData();
        foreach (string sort in sorts)
        {
            if(sort.Trim().Equals("AddressCounty", StringComparison.InvariantCultureIgnoreCase))
            {
                if (e.NewSortOrder == GridSortOrder.Descending)
                {
                    data = data.OrderByDescending(x => x.AddressCounty);
                }
                else
                {
                    data = data.OrderBy(x => x.AddressCounty);
                }
            }
            else if (sort.Trim().Equals("AddressTown", StringComparison.InvariantCultureIgnoreCase))
            {
                if (e.NewSortOrder == GridSortOrder.Descending)
                {
                    data = data.OrderByDescending(x => x.AddressTown);
                }
                else
                {
                    data = data.OrderBy(x => x.AddressTown);
                }
            }
            else if (sort.Trim().Equals("SchoolName", StringComparison.InvariantCultureIgnoreCase))
            {
                if (e.NewSortOrder == GridSortOrder.Descending)
                {
                    data = data.OrderByDescending(x => x.SchoolName);
                }
                else
                {
                    data = data.OrderBy(x => x.SchoolName);
                }
            }
            else if (sort.Trim().Equals("SchoolType", StringComparison.InvariantCultureIgnoreCase))
            {
                if (e.NewSortOrder == GridSortOrder.Descending)
                {
                    data = data.OrderByDescending(x => x.SchoolType);
                }
                else
                {
                    data = data.OrderBy(x => x.SchoolType);
                }
            }
            else if (sort.Trim().Equals("SchoolTel", StringComparison.InvariantCultureIgnoreCase))
            {
                if (e.NewSortOrder == GridSortOrder.Descending)
                {
                    data = data.OrderByDescending(x => x.SchoolTel);
                }
                else
                {
                    data = data.OrderBy(x => x.SchoolTel);
                }
            }
            else if (sort.Trim().Equals("Address1", StringComparison.InvariantCultureIgnoreCase))
            {
                if (e.NewSortOrder == GridSortOrder.Descending)
                {
                    data = data.OrderByDescending(x => x.Address1);
                }
                else
                {
                    data = data.OrderBy(x => x.Address1);
                }
            }
            else if (sort.Trim().Equals("LastName", StringComparison.InvariantCultureIgnoreCase))
            {
                if (e.NewSortOrder == GridSortOrder.Descending)
                {
                    data = data.OrderByDescending(x => x.LastName);
                }
                else
                {
                    data = data.OrderBy(x => x.LastName);
                }
            }
            else if (sort.Trim().Equals("FirstName", StringComparison.InvariantCultureIgnoreCase))
            {
                if (e.NewSortOrder == GridSortOrder.Descending)
                {
                    data = data.OrderByDescending(x => x.FirstName);
                }
                else
                {
                    data = data.OrderBy(x => x.FirstName);
                }
            }
            else if (sort.Trim().Equals("ContactEmail", StringComparison.InvariantCultureIgnoreCase))
            {
                if (e.NewSortOrder == GridSortOrder.Descending)
                {
                    data = data.OrderByDescending(x => x.ContactEmail);
                }
                else
                {
                    data = data.OrderBy(x => x.ContactEmail);
                }
            }
            else if (sort.Trim().Equals("ContactTel", StringComparison.InvariantCultureIgnoreCase))
            {
                if (e.NewSortOrder == GridSortOrder.Descending)
                {
                    data = data.OrderByDescending(x => x.ContactTel);
                }
                else
                {
                    data = data.OrderBy(x => x.ContactTel);
                }
            }
            else if (sort.Trim().Equals("SecondaryContactName", StringComparison.InvariantCultureIgnoreCase))
            {
                if (e.NewSortOrder == GridSortOrder.Descending)
                {
                    data = data.OrderByDescending(x => x.SecondaryContactName);
                }
                else
                {
                    data = data.OrderBy(x => x.SecondaryContactName);
                }
            }
            else if (sort.Trim().Equals("Created", StringComparison.InvariantCultureIgnoreCase))
            {
                if (e.NewSortOrder == GridSortOrder.Descending)
                {
                    data = data.OrderByDescending(x => x.Created);
                }
                else
                {
                    data = data.OrderBy(x => x.Created);
                }
            }
            else
            {
                throw new ApplicationException(string.Format("string '{0}' not handled", sort));
            }
        }
        GridHeaderItem header = e.Item as GridHeaderItem;
        grid.DataSource = data;
        grid.Rebind();
    }

    void download_DataBinding(object sender, EventArgs e)
    {
        download.DataSource = GetData();
    }

    void grid_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        grid.DataSource = GetData();
    }

    protected IEnumerable<ClimateChangeChampionsData.School> GetData()
    {
        ClimateChangeChampionsData.DataContext context = new ClimateChangeChampionsData.DataContext();
        return ClimateChangeChampionsData.School.GetAll(context, null).AsEnumerable();
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    /*protected void dsUsers_UpdatingData(object sender, Acms.DataControls.UpdatingDataEventArgs<User> e)
    {
        Telerik.Web.UI.GridDataItem editItem = gridSchools.EditItems[0] as Telerik.Web.UI.GridDataItem;
        if (editItem != null)
        {
            TextBox txtSchoolName = editItem.EditFormItem.FindControl("txtSchoolName") as TextBox;
            if (txtSchoolName != null)
            {
                if (e.Item.GetCustomProperty("School Name") != txtSchoolName.Text.Trim())
                {
                    e.Item.SetCustomProperty("School Name", txtSchoolName.Text.Trim());
                }
            }

            DropDownList ddlType = editItem.EditFormItem.FindControl("ddlType") as DropDownList;
            if (ddlType != null)
            {
                if (e.Item.GetCustomProperty("School Type") != ddlType.SelectedValue)
                {
                    e.Item.SetCustomProperty("School Type", ddlType.SelectedValue);
                }
            }

            TextBox txtSchoolTel = editItem.EditFormItem.FindControl("txtSchoolTel") as TextBox;
            if (txtSchoolTel != null)
            {
                if (e.Item.GetCustomProperty("School Tel") != txtSchoolTel.Text.Trim())
                {
                    e.Item.SetCustomProperty("School Tel", txtSchoolTel.Text.Trim());
                }
            }

            TextBox txtSecondaryContact = editItem.EditFormItem.FindControl("txtSecondaryContact") as TextBox;
            if (txtSecondaryContact != null)
            {
                if (e.Item.GetCustomProperty("Secondary Contact Name") != txtSecondaryContact.Text.Trim())
                {
                    e.Item.SetCustomProperty("Secondary Contact Name", txtSecondaryContact.Text.Trim());
                }
            }
        }

    }*/

    /*protected void dsUsers_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.User> e)
    {
        List<User> users = Acms.Bll.User.Search(AcmsSite.Id);
        List<User> filtered = (from u in users where !string.IsNullOrEmpty(u.GetCustomProperty("School Name")) select u).ToList();
        Acms.Bll.User.Sort(filtered, gridSchools.MasterTableView.SortExpressions.GetSortString());
        e.SetData(filtered);
    }*/

    /*protected void download_FieldValueDataBound(object sender, Acms.WebControls.DownloadingGrid.FieldValueDataBoundEventArgs e)
    {
        User data = e.DataItem as User;
        if (data != null)
        {
            if (e.Field.HeaderText == "School Name")
            {
                e.Value = data.GetCustomProperty("School Name");
            }
            else if (e.Field.HeaderText == "School Type")
            {
                e.Value = data.GetCustomProperty("School Type");
            }
            else if (e.Field.HeaderText == "School Tel")
            {
                e.Value = data.GetCustomProperty("School Tel");
            }
            else if (e.Field.HeaderText == "Secondary Contact Name")
            {
                e.Value = data.GetCustomProperty("Secondary Contact Name");
            }
        }
    }*/

}
